package com.bsy.framework.utils

import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale

/**
 * Created by Lucy on 2023/10/24.
 * Copyright
 * 描述 :时间类
 */
object TimeHelper {
    const val Y_M = "yyyy.MM"
    const val Y_M2 = "yyyy-MM"
    const val Y_M_D2 = "yyyy.MM.dd"
    const val Y_M_Ds = "yyyy-MM-dd HH:mm:ss"
    const val Y_M_Dm = "yyyy-MM-dd HH:mm"
    const val Y_M_D = "yyyy-MM-dd"
    const val H_M = "HH:mm"
    const val M_D2 = "MM.dd"
    const val HH_mm = "HH:mm"

    /**
     * 获取年月日时分秒
     */
    fun formatTime(timestamp: Long, pattern: String = Y_M_Ds): String {
        val dateFormat = SimpleDateFormat(pattern, Locale.getDefault())
        val date = Date(timestamp)
        return dateFormat.format(date)
    }

    /**
     * 获取年月日
     */
    fun formatTimeYMD(time: String, pattern: String = Y_M_D): String {
        if (time.isEmpty()) {
            return ""
        }
        val dateFormat = SimpleDateFormat(Y_M_Ds, Locale.getDefault())
        val date = dateFormat.parse(time)
        val timestamp = date.time
        val dateFormat1 = SimpleDateFormat(pattern, Locale.getDefault())
        val date1 = Date(timestamp)
        return dateFormat1.format(date1)
    }
    /**
     * 获取时分
     */
    fun formatTimeHm(time: String, pattern: String = HH_mm): String {
        if (time.isEmpty()) {
            return ""
        }
        val dateFormat = SimpleDateFormat(Y_M_Ds, Locale.getDefault())
        val date = dateFormat.parse(time)
        val timestamp = date.time
        val dateFormat1 = SimpleDateFormat(pattern, Locale.getDefault())
        val date1 = Date(timestamp)
        return dateFormat1.format(date1)
    }
    /**
     *  今日零点时间
     */
    fun getTodayZero(): String {
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.SECOND, 0)
        calendar.set(Calendar.MILLISECOND, 0)
        val formatter = SimpleDateFormat(Y_M_Ds, Locale.getDefault())
        return formatter.format(calendar.time)
    }

    /**
     *  今日零点时间
     */
    fun getTodayEnd(): String {
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.HOUR_OF_DAY, 23)
        calendar.set(Calendar.MINUTE, 59)
        calendar.set(Calendar.SECOND, 59)
        calendar.set(Calendar.MILLISECOND, 59)
        val formatter = SimpleDateFormat(Y_M_Ds, Locale.getDefault())
        return formatter.format(calendar.time)
    }

    /**
     * 获取今日到多少天的0点开始
     */
    fun getZeroTime(day: Int): String {
        // 创建 Calendar 实例并设置为当前日期
        val calendar = Calendar.getInstance()
        // 将时间往前推7天
        calendar.add(Calendar.DAY_OF_YEAR, day)
        // 设置时间为7天前的0点0分0秒
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.SECOND, 0)
        val startTime = calendar.time
        // 格式化日期的格式
        val dateFormat = SimpleDateFormat(Y_M_Ds, Locale.getDefault())
        // 格式化开始时间和结束时间
        return dateFormat.format(startTime)
    }

    /**
     * 获取日期0点开始
     */
    fun getYearMonthDayEndTime(year: Int, month: Int, day: Int): String {
        // 创建 Calendar 实例并设置为当前日期
        val calendar = Calendar.getInstance()
        calendar.set(year, month - 1, day, 23, 59, 59)
        val startTime = calendar.time
        // 格式化日期的格式
        val dateFormat = SimpleDateFormat(Y_M_Ds, Locale.getDefault())
        // 格式化开始时间和结束时间
        return dateFormat.format(startTime)
    }

    /**
     * 获取日期结束
     */
    fun getYearMonthDayZeroTime(year: Int, month: Int, day: Int): String {
        // 创建 Calendar 实例并设置为当前日期
        val calendar = Calendar.getInstance()
        calendar.set(year, month - 1, day, 0, 0, 0)
        val startTime = calendar.time
        // 格式化日期的格式
        val dateFormat = SimpleDateFormat(Y_M_Ds, Locale.getDefault())
        // 格式化开始时间和结束时间
        return dateFormat.format(startTime)
    }
}